package com.sunny.shop.service;

import com.github.dozermapper.core.DozerBeanMapper;
import com.github.dozermapper.core.DozerBeanMapperBuilder;
import com.github.dozermapper.core.Mapper;
import com.sunny.shop.dto.StoryDTO;
import com.sunny.shop.model.Song;
import com.sunny.shop.model.Story;
import com.sunny.shop.repository.StoryRepository;
import com.sunny.shop.vo.SongVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StoryServiceImpl {
    @Autowired
    private StoryRepository storyRepository;

    @Value("${sunny.oss.url}")
    private String baseUrl;
    public Page<Story> getStorys(Long cate_id, Integer pageNum,Integer size){
        Pageable page = PageRequest.of(pageNum, size);

        Page<Story> all = storyRepository.findAllByCateId(cate_id,page);
        return all;
    }

    public ArrayList<SongVO> getStoryDetail(Long id){
        Story storeDetail = storyRepository.findAllById(id);
        List<Song> songList = storeDetail.getSongList();
        Mapper mapper = DozerBeanMapperBuilder.buildDefault();
        ArrayList<SongVO> arrayList = new ArrayList<>();
        songList.forEach(s->{
            String orgPlayUrl = s.getPlayUrl();
            String newPlayUrl = baseUrl + "/" + orgPlayUrl;
            s.setPlayUrl(newPlayUrl);
            SongVO songVO = new SongVO();
            mapper.map(s, songVO);
            songVO.setIsCollect(false);
            arrayList.add(songVO);
        });
        return arrayList;
    }
}
